use rspc::Type;
use serde::Serialize;

use crate::config::{ConfigArg, ConfigManager, SaveConfigData};

use super::{RouterBuilder, utils::LibraryRequest};

pub(crate) fn mount() -> RouterBuilder {
    return RouterBuilder::new()
        .query("version", |t| {
            #[derive(Serialize, Type)]
			pub struct BuildInfo {
				version: &'static str,
			}

			t(|_, _: ()| BuildInfo {
				version: "0.0.1",
			})
        })
        .library_query("key", |t| {
            t(|_, args: ConfigArg, library| async move {
                Ok(ConfigManager::get(library.db, args).await?)
            })
        })
        .library_query("all", |t| {
            t(|_, _: (), library| async move {
                Ok(ConfigManager::all(library.db).await?)
            })
        })
        .library_mutation("save", |t| {
            t(|_, args: SaveConfigData, library| async move {
                Ok(ConfigManager::save(library.db, args).await?)
            })
        });
}
